package de.steric.battlemon.collision;

import java.util.ArrayList;

import org.lwjgl.util.vector.Vector3f;

import de.steric.MathHelper;
import de.steric.battlemon.GameObject;
import de.steric.battlemon.GameObject;

public class collisionObjectGroup extends collisionGroup {
	public ArrayList<GameObject> groupItems;

	public collisionObjectGroup(GameObject first) {
		groupItems = new ArrayList<GameObject>();
		groupItems.add(first);
	}

	@Override
	public void createGroupCollider() {
		// size in X Direction
		float maxX = this.groupItems.get(0).collider.getboundingVerticeAt(0).x;
		float minX = this.groupItems.get(0).collider.getboundingVerticeAt(0).x;
		// size in Y Direction
		float maxY = this.groupItems.get(0).collider.getboundingVerticeAt(0).y;
		float minY = this.groupItems.get(0).collider.getboundingVerticeAt(0).y;

		// size in Z Direction
		float maxZ = this.groupItems.get(0).collider.getboundingVerticeAt(0).z;
		float minZ = this.groupItems.get(0).collider.getboundingVerticeAt(0).z;

		for (GameObject element : groupItems) {
			for (int i = 0; i < 8; i++) {
				// X
				maxX = Math.max(maxX,
						element.collider.getboundingVerticeAt(i).x);
				minX = Math.min(minX,
						element.collider.getboundingVerticeAt(i).x);

				// Y
				maxY = Math.max(maxY,
						element.collider.getboundingVerticeAt(i).y);
				minY = Math.min(minY,
						element.collider.getboundingVerticeAt(i).y);

				// Z
				maxZ = Math.max(maxZ,
						element.collider.getboundingVerticeAt(i).z);
				minZ = Math.min(minZ,
						element.collider.getboundingVerticeAt(i).z);
			}
		}

		this.groupCollider.setboundingVerticeAt(0, new Vector3f(minX, minY,
				minZ));
		this.groupCollider.setboundingVerticeAt(1, new Vector3f(maxX, minY,
				minZ));
		this.groupCollider.setboundingVerticeAt(2, new Vector3f(maxX, minY,
				maxZ));
		this.groupCollider.setboundingVerticeAt(3, new Vector3f(minX, minY,
				maxZ));
		this.groupCollider.setboundingVerticeAt(4, new Vector3f(minX, maxY,
				minZ));
		this.groupCollider.setboundingVerticeAt(5, new Vector3f(maxX, maxY,
				minZ));
		this.groupCollider.setboundingVerticeAt(6, new Vector3f(maxX, maxY,
				maxZ));
		this.groupCollider.setboundingVerticeAt(7, new Vector3f(minX, maxY,
				maxZ));
	}

	@Override
	public boolean addItem(GameObject newItem) {
		if (MathHelper.Distance(newItem, groupItems.get(0)) <= 10f) {
			groupItems.add(newItem);
			return true;
		}
		return false;
	}

	public boolean isEmpty() {
		if (groupItems.isEmpty()) {
			return true;
		}
		return false;
	}

	@Override
	ArrayList<GameObject> isCollision(GameObject other) {
		ArrayList<GameObject> collisions = new ArrayList<GameObject>();
		if (this.groupCollider.isCollision(other.collider)) {
			for (GameObject element : groupItems) {
				if (element.collider.isCollision(other.collider)) {
					collisions.add(element);
				}
			}
		}
		return collisions;
	}
}
